package cn.kxm.service.impl;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import cn.kxm.dao.InstroageBillDao;
import cn.kxm.dao.InstroageBillDetailMapper;
import cn.kxm.dao.InstroageBillMapper;
import cn.kxm.dao.PayBillDetailDao;
import cn.kxm.pojo.InstroageBill;
import cn.kxm.pojo.InstroageBillDetail;
import cn.kxm.pojo.Storageinfo;
import cn.kxm.service.InstroageBillDetailService;
import cn.kxm.service.InstroageBillService;
import cn.kxm.service.PurchaseOrderBillService;
import cn.kxm.service.StorageInfoService;
@Service
public class InstroageBillServiceImpl implements InstroageBillService{

	//User by instroagebill
	@Autowired
	private InstroageBillMapper instroageBillMapper;
    @Autowired
    private InstroageBillDetailService detailService;
    @Autowired
    private InstroageBillDetailMapper detailMapper;
    @Autowired
    private StorageInfoService infoService;
    @Autowired
    private PurchaseOrderBillService orderBillService;
    
	@Override
	public int findInstroageBillCount() {
		return instroageBillMapper.findInstroageBillCount();
	}
	@Override
	public List<InstroageBill> getInstroageBillList(Integer page,
			Integer size, String findcode, Date findstartDate,
			Date findendDate, String findsupplierName) {
		int currPage=(page-1)*size;
		return instroageBillMapper.getInstroageBillList(currPage, size, findcode, findstartDate, findendDate, findsupplierName);
	}
	@Override
	public boolean addInstroageBill(String inBill,String codes,Double total) {
		JSONObject inBillJson=JSONObject.parseObject(inBill);
		InstroageBill instroageBill=new InstroageBill();
		instroageBill.setCode((String)inBillJson.get("code"));
		instroageBill.setCustomName((String)inBillJson.get("customName"));
		try {
			instroageBill.setInstroageDate(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse((String)inBillJson.get("instroageDate")));
		} catch (ParseException e) {
			e.printStackTrace();
		}
		instroageBill.setInstroageType(inBillJson.getInteger("instroageType"));
		instroageBill.setLinkman((String)inBillJson.get("linkman"));
		instroageBill.setMemo((String)inBillJson.get("memo"));
		instroageBill.setPhone((String)inBillJson.get("phone"));
		instroageBill.setMoney(total);
		if(instroageBillMapper.addInstroageBill(instroageBill)!=1){
			return false;
		}
		JSONArray backBodyJson = JSONArray.parseArray(codes);
		for(int i=0;i<backBodyJson.size();i++){
			InstroageBillDetail detail=new InstroageBillDetail();
			JSONObject code=backBodyJson.getJSONObject(i);
			detail.setAmount(code.getIntValue("amount"));
			detail.setBillCode((String)instroageBill.getCode());
			detail.setMemo((String)code.get("memo"));
			detail.setPartCode((String)code.get("code"));
			detail.setPrice(code.getDoubleValue("price"));
			detail.setOrderCode(code.getString("orderCode"));
			if (detailService.addInstroageBillDetail(detail)!=1) {
				return false;
			}
			
		}
		
		return true;
	}
	@Override
	public InstroageBill getInstroageBillByCode(String code) {
		return instroageBillMapper.getInstroageBillByCode(code);
	}
	@Override
	public boolean modifyInstroageBill(String inBill,String codes,Double total) {
		JSONObject inBillJson=JSONObject.parseObject(inBill);
		InstroageBill instroageBill=new InstroageBill();
		instroageBill.setCode((String)inBillJson.get("code"));
		instroageBill.setCustomName((String)inBillJson.get("customName"));
		try {
			instroageBill.setInstroageDate(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse((String)inBillJson.get("instroageDate")));
		} catch (ParseException e) {
			e.printStackTrace();
		}
		instroageBill.setInstroageType(inBillJson.getInteger("instroageType"));
		instroageBill.setLinkman((String)inBillJson.get("linkman"));
		instroageBill.setMemo((String)inBillJson.get("memo"));
		instroageBill.setPhone((String)inBillJson.get("phone"));
		instroageBill.setMoney(total);
		if(instroageBillMapper.updateInstroageBill(instroageBill)!=1){
			return false;
		}
		if(detailService.delDetailByUpdate(instroageBill.getCode())<=0){
			return false;
		}
		JSONArray backBodyJson = JSONArray.parseArray(codes);
		for(int i=0;i<backBodyJson.size();i++){
			InstroageBillDetail detail=new InstroageBillDetail();
			JSONObject code=backBodyJson.getJSONObject(i);
			/*if(code.get("amount") instanceof String){
				detail.setAmount(Integer.valueOf((String)code.get("amount")));
			}else{
				detail.setAmount((Integer)code.get("amount"));
			}*/
			detail.setAmount(code.getIntValue("amount"));
			detail.setBillCode((String)instroageBill.getCode());
			detail.setMemo((String)code.get("memo"));
			detail.setPartCode((String)code.get("code"));
			detail.setPrice(code.getDoubleValue("price"));
			detail.setOrderCode(code.getString("orderCode"));
			System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"+detail.getOrderCode());
			if (detailService.addInstroageBillDetail(detail)!=1) {
				return false;
			}
		}
		return true;
	}
	@Override
	public boolean delInstroageBillCode(String code) {
		if(detailService.delInstroageBillDetailByBillCode(code)<=0){
			return false;
		}
		if(instroageBillMapper.delInstroageBillCode(code)<=0){
			return false;
		}
		return true;
	}
	@Override
	public boolean delInstroageBillByArray(String[] codes) {
		if(detailMapper.removeInstroageBillDetailByArray(codes)<=0){
			return false;
		}
		if(instroageBillMapper.delInstroageBillByArray(codes)<=0){
			return false;
		}
		return true;
	}
	
		@Override
	public boolean modifyInstroageBillCheckStatus(String code) {
		List<Map<String, Object>> maps=detailService.findDetailByCode(code);
		List<String> orderCodes=new ArrayList<String>();
		String codeBridge=null;
		for (Map<String, Object> map : maps) {
			String code1=(String)map.get("billCode");
			if(code1!=null&&!code1.equals("")){
				codeBridge=code1;
				orderCodes.add(code1);
			}
			if(!code1.equals(codeBridge)&&code1!=null&&!code1.equals("")){
				orderCodes.add(code1);
			}
			String partCode=(String)map.get("partCode");
			Integer amount=(Integer)map.get("amount");
			Float price=(Float)map.get("price");
			Storageinfo info=infoService.getStorageinfoByPartCode(partCode);
			Storageinfo newInfo=new Storageinfo();
			Integer infoAmount=info.getAmount()+amount;
			Double infoPrice=(info.getStroragePrice()*info.getAmount()+amount*price)/infoAmount;
			newInfo.setAmount(infoAmount);
			newInfo.setStroragePrice(infoPrice);
			newInfo.setPartCode(partCode);
			if(infoService.updateStorageInfoByPartCode(newInfo)<=0){
				return false;
			}
		}
		System.out.println(orderCodes.size()+"................................");
		if(orderCodes.size()!=0){
			if(orderBillService.updateOrderInstroageStatus(orderCodes,2)<=0){
				return false;
			}
		}
		if(instroageBillMapper.updateInstroageBillCheckStatus(code,2)<=0){
			return false;
		}
		return true;
	}
	@Override
	public boolean modifyCancelInstroageBillCheckStatus(String billCode) {
		List<Map<String, Object>> maps=detailService.findDetailByCode(billCode);
		List<String> orderCodes=new ArrayList<String>();
		String codeBridge="";
		for (Map<String, Object> map : maps) {
			String code1=(String)map.get("billCode");
			if(code1!=null&&!code1.equals("")){
				codeBridge=code1;
				orderCodes.add(code1);
			}
			if(!codeBridge.equals(code1)&&code1!=null&&!code1.equals("")){
				orderCodes.add(code1);
			}
			String partCode=(String)map.get("partCode");
			Integer amount=(Integer)map.get("amount");
			Float price=(Float)map.get("price");
			Storageinfo info=infoService.getStorageinfoByPartCode(partCode);
			Storageinfo newInfo=new Storageinfo();
			Integer infoAmount=info.getAmount()-amount;
			Double infoPrice=(info.getStroragePrice()*info.getAmount()-amount*price)/infoAmount;
			newInfo.setAmount(infoAmount);
			newInfo.setStroragePrice(infoPrice);
			newInfo.setPartCode(partCode);
			if(infoService.updateStorageInfoByPartCode(newInfo)<=0){
				return false;
			}
		}
		if(orderCodes.size()!=0){
			if(orderBillService.updateOrderInstroageStatus(orderCodes,1)<=0){
				return false;
			}
		}
		if(instroageBillMapper.updateInstroageBillCheckStatus(billCode,1)<=0){
			return false;
		}
		return true;
	}

	//User by payBill
	@Autowired
	InstroageBillDao instroageBillDao;

	@Autowired
	PayBillDetailDao payBillDetailDao;

	/* (non-Javadoc)
	 * @see cn.kxm.service.InstroageBillService#findPayBillDetail(java.lang.String)
	 */
	@Override
	public InstroageBill findPayBillDetail(String payCode) {
		return instroageBillDao.findInstroageBillByBillCode(payCode);
	}

	/* (non-Javadoc)
	 * @see cn.kxm.service.InstroageBillService#updateInstroageBillType(java.lang.String, java.lang.Integer)
	 */
	@Override
	public void updateInstroageBillType(String code, Integer type) {
		instroageBillDao.updateInstroageBillType(code, type);
	}

	/* (non-Javadoc)
	 * @see cn.kxm.service.InstroageBillService#findUnselectedInstroageBill(java.lang.Integer, java.lang.Integer)
	 */
	@Override
	public List<InstroageBill> findUnselectedInstroageBill(Integer page, Integer rows,
			String findcode,
			String findstartDate,
			String findendDate,
			String findsupplierName,
			String payCode) {
		return instroageBillDao.findUnselectedInstroageBill(((page-1)*rows), rows,findcode,findstartDate,findendDate,findsupplierName,payCode);
	}

	/* (non-Javadoc)
	 * @see cn.kxm.service.InstroageBillService#countUnselectedInstroageBill()
	 */
	@Override
	public int countUnselectedInstroageBill(String findcode,
			String findstartDate,
			String findendDate,
			String findsupplierName,
			String payCode) {
		return instroageBillDao.countUnselectedInstroageBill(findcode,findstartDate,findendDate,findsupplierName,payCode);
	}

	/* (non-Javadoc)
	 * @see cn.kxm.service.InstroageBillService#findInstroageBillByPayBillCode(java.lang.String, java.lang.Integer, java.lang.Integer)
	 */
	@Override
	public List<InstroageBill> findInstroageBillByPayBillCode(String payCode) {
		List<String> list=payBillDetailDao.findBillCodeByPayCode(payCode);
		return instroageBillDao.findInstroageBillByPayBillCode(list);


	}
	/* (non-Javadoc)
	 * @see cn.kxm.service.InstroageBillService#findOrderCodeFromInstroDetail(java.lang.String)
	 */
	@Override
	public List<String> findOrderCodeFromInstroDetail(String billCode) {
		return instroageBillDao.findOrderCodeFromInstroDetail(billCode);
	}
	/* (non-Javadoc)
	 * @see cn.kxm.service.InstroageBillService#updatePurchasePayStatus(java.util.List)
	 */
	@Override
	public void updatePurchasePayStatus(List<String> list,int type) {
		instroageBillDao.updatePurchasePayStatus(list,type);
	}
}
